home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 October / maximum-cd-2009-10.iso / DiscContents / Firefox Setup 3.5.exe / nonlocalized / chrome / toolkit.jar / content / global / commonDialog.js < prev    next >
Encoding:
JavaScript  |  2009-06-24  |  11.2 KB  |  342 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 1998
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Alec Flett  <alecf@netscape.com>
  23.  *   Ben Goodger <ben@netscape.com>
  24.  *   Blake Ross  <blakeross@telocity.com>
  25.  *   Joe Hewitt <hewitt@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
  42. var gCommonDialogParam = 
  43.   window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  44.   
  45. function showControls()
  46. {
  47.   // This is called before onload fires, so we can't be certain that any elements
  48.   // in the document have their bindings ready, so don't call any methods/properties
  49.   // here on xul elements that come from xbl bindings.
  50.   
  51.   // show the required textboxes and set their initial values
  52.   var nTextBoxes = gCommonDialogParam.GetInt(3);
  53.   if (nTextBoxes == 2) {
  54.     if (gCommonDialogParam.GetInt(4) == 1) {
  55.       initTextbox("password1", 4, 6, false);
  56.       initTextbox("password2", 5, 7, false);
  57.     }
  58.     else {
  59.       initTextbox("login", 4, 6, false);
  60.       initTextbox("password1", 5, 7, false);
  61.     }
  62.   } else if (nTextBoxes == 1) {
  63.     if (gCommonDialogParam.GetInt(4) == 1)
  64.       initTextbox("password1", -1, 6, true);
  65.     else
  66.       initTextbox("login", 4, 6, true);
  67.   }
  68. }
  69.  
  70. function setLabelForNode(aNode, aLabel, aIsLabelFlag)
  71. {
  72.   // This is for labels which may contain embedded access keys.
  73.   // If we end in (&X) where X represents the access key, optionally preceded
  74.   // by spaces and/or followed by the ':' character, store the access key and
  75.   // remove the access key placeholder + leading spaces from the label.
  76.   // Otherwise a character preceded by one but not two &s is the access key.
  77.   // Store it and remove the &.
  78.  
  79.   // Note that if you change the following code, see the comment of
  80.   // nsTextBoxFrame::UpdateAccessTitle.
  81.   var accessKey = null;
  82.   if (/ *\(\&([^&])\)(:)?$/.test(aLabel)) {
  83.     aLabel = RegExp.leftContext + RegExp.$2;
  84.     accessKey = RegExp.$1;
  85.   } else if (/^(.*[^&])?\&(([^&]).*$)/.test(aLabel)) {
  86.     aLabel = RegExp.$1 + RegExp.$2;
  87.     accessKey = RegExp.$3;
  88.   }
  89.  
  90.   // && is the magic sequence to embed an & in your label.
  91.   aLabel = aLabel.replace(/\&\&/g, "&");
  92.   if (aIsLabelFlag) {    // Set text for <label> element
  93.     aNode.setAttribute("value", aLabel);
  94.   } else {    // Set text for other xul elements
  95.     aNode.label = aLabel;
  96.   }
  97.  
  98.   // XXXjag bug 325251
  99.   // Need to set this after aNode.setAttribute("value", aLabel);
  100.   if (accessKey)
  101.     aNode.accessKey = accessKey;
  102. }
  103.  
  104. function commonDialogOnLoad()
  105. {
  106.   // set the document title
  107. //@line 110 "e:\builds\moz2_slave\win32_build\build\toolkit\content\commonDialog.js"
  108.   document.title = gCommonDialogParam.GetString(12);
  109. //@line 112 "e:\builds\moz2_slave\win32_build\build\toolkit\content\commonDialog.js"
  110.  
  111.   // set the number of command buttons
  112.   var nButtons = gCommonDialogParam.GetInt(2);
  113.   var dialog = document.documentElement;
  114.   switch (nButtons) {
  115.     case 1:
  116.       dialog.getButton("cancel").hidden = true;
  117.       break;
  118.     case 4:
  119.       dialog.getButton("extra2").hidden = false;
  120.     case 3:
  121.       dialog.getButton("extra1").hidden = false;
  122.   }
  123.  
  124.   // display the main text
  125.   // XXX the substr(0, 10000) part is a workaround for bug 317334
  126.   var croppedMessage = gCommonDialogParam.GetString(0).substr(0, 10000);
  127.   setElementText("info.body", croppedMessage, true);
  128.  
  129.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  130.  
  131.   // set the icon
  132.   var iconElement = document.getElementById("info.icon");
  133.   var iconClass = gCommonDialogParam.GetString(2);
  134.   if (!iconClass)
  135.     iconClass = "message-icon";
  136.   iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
  137.  
  138.   switch (nButtons) {
  139.     case 4:
  140.       setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
  141.       // fall through
  142.     case 3:
  143.       setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
  144.       // fall through
  145.     default:
  146.     case 2:
  147.       var string = gCommonDialogParam.GetString(9);
  148.       if (string)
  149.         setLabelForNode(document.documentElement.getButton("cancel"), string);
  150.       // fall through
  151.     case 1:
  152.       string = gCommonDialogParam.GetString(8);
  153.       if (string)
  154.         setLabelForNode(document.documentElement.getButton("accept"), string);
  155.       break;
  156.   }
  157.  
  158.   // set default result to cancelled
  159.   gCommonDialogParam.SetInt(0, 1); 
  160.  
  161.   // initialize the checkbox
  162.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  163.  
  164.   if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
  165.   {
  166.     var dlgButtons = ['accept', 'cancel', 'extra1', 'extra2'];
  167.  
  168.     // Set the default button and focus it on non-OS X systems
  169.     var dButton = dlgButtons[gCommonDialogParam.GetInt(5)];
  170.     document.documentElement.defaultButton = dButton;
  171. //@line 174 "e:\builds\moz2_slave\win32_build\build\toolkit\content\commonDialog.js"
  172.     document.documentElement.getButton(dButton).focus();
  173. //@line 176 "e:\builds\moz2_slave\win32_build\build\toolkit\content\commonDialog.js"
  174.   }
  175.  
  176.   if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
  177.   {
  178.     var delayInterval = 2000;
  179.     try {
  180.       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  181.                   .getService(Components.interfaces.nsIPrefBranch);
  182.       delayInterval = prefs.getIntPref("security.dialog_enable_delay");
  183.     } catch (e) {}
  184.  
  185.     document.documentElement.getButton("accept").disabled = true;
  186.     document.documentElement.getButton("extra1").disabled = true;
  187.     document.documentElement.getButton("extra2").disabled = true;
  188.  
  189.     setTimeout(commonDialogReenableButtons, delayInterval);
  190.     
  191.     addEventListener("blur", commonDialogBlur, false);
  192.     addEventListener("focus", commonDialogFocus, false);
  193.   }
  194.  
  195.   getAttention();
  196.  
  197.   // play sound
  198.   try {
  199.     var sound = gCommonDialogParam.GetString(13);
  200.     if (sound) {
  201.       Components.classes["@mozilla.org/sound;1"]
  202.                 .createInstance(Components.interfaces.nsISound)
  203.                 .playSystemSound(sound);
  204.     }
  205.   } catch (e) { }
  206. }
  207.  
  208. var gDelayExpired = false;
  209. var gBlurred = false;
  210.  
  211. function commonDialogBlur(aEvent)
  212. {
  213.   if (aEvent.target != document)
  214.     return;
  215.   gBlurred = true;
  216.   document.documentElement.getButton("accept").disabled = true;
  217.   document.documentElement.getButton("extra1").disabled = true;
  218.   document.documentElement.getButton("extra2").disabled = true;
  219. }
  220.  
  221. function commonDialogFocus(aEvent)
  222. {
  223.   if (aEvent.target != document)
  224.     return;
  225.   gBlurred = false;
  226.   // When refocusing the window, don't enable the buttons unless the countdown
  227.   // delay has expired. 
  228.   if (gDelayExpired) {
  229.     var script = "document.documentElement.getButton('accept').disabled = false; ";
  230.     script += "document.documentElement.getButton('extra1').disabled = false; ";
  231.     script += "document.documentElement.getButton('extra2').disabled = false;";
  232.     setTimeout(script, 250);
  233.   }
  234. }
  235.  
  236. function commonDialogReenableButtons()
  237. {
  238.   // Don't automatically enable the buttons if we're not in the foreground
  239.   if (!gBlurred) {
  240.     document.documentElement.getButton("accept").disabled = false;
  241.     document.documentElement.getButton("extra1").disabled = false;
  242.     document.documentElement.getButton("extra2").disabled = false;
  243.   }
  244.   gDelayExpired = true;
  245. }
  246.  
  247. function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
  248. {
  249.   unHideElementById(aName+"Container");
  250.  
  251.   var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
  252.   if (label || aAlwaysLabel && !label)
  253.     setElementText(aName+"Label", label);
  254.     
  255.   var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
  256.   var textbox = document.getElementById(aName + "Textbox");
  257.   textbox.setAttribute("value", value);
  258. }
  259.  
  260. function setElementText(aElementID, aValue, aChildNodeFlag)
  261. {
  262.   var element = document.getElementById(aElementID);
  263.   if (!aChildNodeFlag && element) {
  264.     setLabelForNode(element, aValue, true);
  265.   } else if (aChildNodeFlag && element) {
  266.     element.appendChild(document.createTextNode(aValue));
  267.   }
  268. }
  269.  
  270. function setCheckbox(aChkMsg, aChkValue)
  271. {
  272.   if (aChkMsg) {
  273.     unHideElementById("checkboxContainer");
  274.     
  275.     var checkboxElement = document.getElementById("checkbox");
  276.     setLabelForNode(checkboxElement, aChkMsg);
  277.     checkboxElement.checked = aChkValue > 0;
  278.   }
  279. }
  280.  
  281. function unHideElementById(aElementID)
  282. {
  283.   var element = document.getElementById(aElementID);
  284.   element.hidden = false;
  285. }
  286.  
  287. function hideElementById(aElementID)
  288. {
  289.   var element = document.getElementById(aElementID)
  290.   element.hidden = true;
  291. }
  292.  
  293. function isVisible(aElementId)
  294. {
  295.   return document.getElementById(aElementId).hasAttribute("hidden");
  296. }
  297.  
  298. function onCheckboxClick(aCheckboxElement)
  299. {
  300.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  301. }
  302.  
  303. function commonDialogOnAccept()
  304. {
  305.   gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
  306.  
  307.   var numTextBoxes = gCommonDialogParam.GetInt(3);
  308.   var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
  309.   
  310.   if (numTextBoxes >= 1) {
  311.     var editField1;
  312.     if (textboxIsPassword1)
  313.       editField1 = document.getElementById("password1Textbox");
  314.     else
  315.       editField1 = document.getElementById("loginTextbox");
  316.     gCommonDialogParam.SetString(6, editField1.value);
  317.   }
  318.  
  319.   if (numTextBoxes == 2) {
  320.     var editField2;
  321.     if (textboxIsPassword1)
  322.       // we had two password fields
  323.       editField2 = document.getElementById("password2Textbox");
  324.     else
  325.       // we only had one password field (and one login field)
  326.       editField2 = document.getElementById("password1Textbox");
  327.     gCommonDialogParam.SetString(7, editField2.value);
  328.   }
  329. }
  330.  
  331. function commonDialogOnExtra1()
  332. {
  333.   gCommonDialogParam.SetInt(0, 2);
  334.   window.close();
  335. }
  336.  
  337. function commonDialogOnExtra2()
  338. {
  339.   gCommonDialogParam.SetInt(0, 3);
  340.   window.close();
  341. }
  342.